package com.example.udpDemo.socket.event;

import com.example.udpDemo.ProtocolServiceImpl;
import com.example.udpDemo.socket.udp.NettyPacket;
import com.example.udpDemo.socket.udp.UdpServer;
import io.netty.channel.ChannelId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.gateway.protocolimpl.ProtocolService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: 自定义数据包处理监听器
 *
 * @author sjt Administrator
 * @since 2023/11/28 22:19
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class NettyServerPacketListener {


    @EventListener
    public void onApplicationEvent(NettyPacketEvent event) {
        List<NettyPacket> nettyPackets = event.getNettyPackets();
        log.info("输出到平台:{}", nettyPackets);
        nettyPackets.parallelStream().forEach(nettyPacket -> {
            StringBuffer valueStr = new StringBuffer(nettyPacket.getValue())
                    .insert(nettyPacket.getValue().length() - nettyPacket.getNumPort(), ".");
            ProtocolServiceImpl.PARAM_MAP.put(nettyPacket.getSn(), valueStr.toString());
        });
    }
}
